import './switch.scss';
import templateUrl from './switch.html';

class Directive {
    constructor() {
        this.scope = {
            ngModel: '=',
            disabled: '=',
            size: '@',
            onChange: '&?'
        };
        this.restrict = 'E';
        this.replace = true;
        this.controllerAs = 'vm';
        this.templateUrl = templateUrl;
    }

    controller($scope) {
        "ngInject";
        this.toggle = ()=> {
            if ($scope.disabled === true) {
                return;
            }

            $scope.ngModel = !$scope.ngModel;

            if (typeof $scope.onChange === 'function') {
                $scope.onChange({checked: $scope.ngModel});
            }
        };
    }

    static factory() {
        return new Directive();
    }
}

export default Directive;